var knowId = getQueryString("id");
$(function () {
    $("#jqGrid").Grid({
        url: '../annexfile/list?knowId=' + knowId,
        colModel: [
            {label: '附件标题', name: 'fileName', index: 'FILE_NAME'},
            {label: '附件名称', name: 'relName', index: 'REL_NAME'},
            {label: '上传用户', name: 'createUserName', index: 'CREATE_NAME', width: 60},
            {
                label: '上传日期', name: 'uploadDate', index: 'UPLOAD_DATE', width: 80, align: 'center',
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm:ss");
                }
            },
            {
                label: '操作', name: 'url', index: 'URL', width: 40, align: 'center',
                formatter: function (value, col, row) {
                    return "<button title='预览' class='btn btn-outline btn-info' onclick='fileCommonHandle(\"" + value + "\", \"" + row.relName + "\")'><i class='fa fa-eye'></i></button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        annexFile: {},
        ruleValidate: {
            fileName: [
                {required: true, message: '附件名称不能为空', trigger: 'change'}
            ],
            url: [
                {required: true, message: '附件不能为空', trigger: 'change'}
            ]
        },
        q: {
            fileName: ''
        },
        picUrl: "",
        knowTypeList: [] // 知识归类,从知识类型获取
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.annexFile = {
                id: '',
                fileName: '',
                relName: '',
                url: ''
            };
            vm.picUrl = '';
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            if (vm.annexFile.id === (null || '')) {
                Ajax.request({
                    url: "../annexfile/save/" + knowId,
                    params: JSON.stringify(vm.annexFile),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            } else {
                Ajax.request({
                    url: "../annexfile/update",
                    params: JSON.stringify(vm.annexFile),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            }
        } ,
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                // vm.annexFile.delFlag = 0;
                // Ajax.request({
                //     url: "../annexfile/update",
                //     params: JSON.stringify(vm.annexFile),
                //     type: "POST",
                //     contentType: "application/json",
                //     successCallback: function (r) {
                //         alert('操作成功', function (index) {
                //             vm.reload();
                //         });
                //     }
                // });
                // 暂时默认为知识库条目删除
                Ajax.request({
                    url: "../knowfile/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../annexfile/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.annexFile = r.annexFile;
                    var url = vm.annexFile.url;
                    var name = vm.annexFile.fileName;
                    if (url) vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'fileName': vm.q.fileName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                fileName: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleBeforeUpload: function() {
            this.$refs.uploadFile.clearFiles()
            this.$Loading.start();
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的图片。'
            });
        } ,
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            if(res.code == 0){
                this.$Loading.finish();
                iview.Message.success('文件上传成功!');
                vm.annexFile.url = res.url;
                vm.annexFile.fileName = res.fileName;
                vm.annexFile.relName = res.name;
            }else{
                this.$Loading.error();
                iview.Message.error(res.msg);
            }
        } ,
        eyeFile: function (url, filename) {
            if (url) {
                renameAfterDownload(url, filename)
            } else {
                alert('还未上传文件');
            }
        },
        getKonwTypeList: function () {
            Ajax.request({
                url: "../sysknowtype/optionList",
                async: true,
                successCallback: function (r) {
                    vm.knowTypeList = r.optionList;
                }
            });
        }
    },
    created(){
        this.getKonwTypeList();
    }
});
